﻿--[[
   @brief    qq friend dialog
   @author   vincentlao
   @version  1.0
   @date     2015-12-03
--]]

qq_friend_control = 
{
    invite_id_list = {};
    func_map = {};

    qq_friend_list = {};
    qq_group_info = {};
    is_flush_data = false;

    init = function ()
        base_utility.log("[qq_friend_control] init");
        qq_friend_control.func_map["web_init"] = qq_friend_control.web_init;
        qq_friend_control.func_map["open_qq_group"] = qq_friend_control.open_qq_group;
        qq_friend_control.func_map["add_qq_friend"] = qq_friend_control.add_qq_friend;
        qq_friend_control.func_map["show_personal_center"] = qq_friend_control.show_personal_center;
        qq_friend_control.func_map["close"] = qq_friend_control.close;
    end;

    is_show = function(data)
        local bShow = false;
        if qq_friend_view ~= nil then
            bShow = qq_friend_view:is_show();
        end;
        return {is_show = bShow};
    end;

    show = function (event_id, json_str)
        ierd_tgp.tpf_ui.Require("social\\qq_friend\\view.lua");
        qq_friend_view:show(true);
    end;

    on_js_callback = function(uilib, func_name, json_str)
        base_utility.log("[qq_friend_control]on_js_callback, func_name:"..func_name);
        local fun = qq_friend_control.func_map[func_name];
        if fun ~= nil then
            fun(json_str);
        end
    end;

    web_init = function(json_str)
        --get data and push
        if qq_friend_control.is_flush_data then
            qq_friend_control.push_qq_friend_list();
        end
    end;

    --对外服务接口
    flush_data = function(qq_friend_data)
        base_utility.log("[qq_friend_control] flush_data");
        qq_friend_control.is_flush_data = true;
        qq_friend_control.qq_friend_list = qq_friend_data.qq_friend_list
        qq_friend_control.qq_group_info = qq_friend_data.qq_group_info;
        if qq_friend_view ~= nil and qq_friend_view:is_show() then
            qq_friend_control.push_qq_friend_list();
        end
    end;

    push_qq_friend_list = function()
        -- 需求http://tapd.oa.com/TGC/prong/stories/view/1010041181058625317?action_entry_type=stories
        -- 首次启动不显示qq好友列表的小红点
        local out = comm_center.call_service('im_app_service_read_common_cfg', {is_self = 1, key = "is_qq_friend_menu_reddot_first_show"});
        if out["key"] == "is_qq_friend_menu_reddot_first_show" and out["value"] ~= "true" then
            for i = 1, #qq_friend_control.qq_friend_list do
                qq_friend_control.qq_friend_list[i].is_new = 0;
            end
            for i = 1, #qq_friend_control.qq_group_info do
                comm_center.call_service("im_app_service_clear_group_qq_friend_new_status", {qq_group_id = qq_friend_control.qq_group_info[i].group_id});
            end
            comm_center.call_service('im_app_service_write_common_cfg', {is_self = 1, key = "is_qq_friend_menu_reddot_first_show", value = "true"});
        end


        if qq_friend_control.qq_friend_list ~= nil then 
            for index, friend in ipairs(qq_friend_control.qq_friend_list) do
                if social_utility.is_my_friend(friend.friend_id) == true then
                    qq_friend_control.qq_friend_list[index].is_friend = 1;
                    qq_friend_control.qq_friend_list[index].is_new = 0;
                    comm_center.call_service("im_app_service_clear_one_qq_friend_new_status", {qq_friend_id = friend.friend_id});
                else
                    qq_friend_control.qq_friend_list[index].is_friend = 0;
                end
            end
        end
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {}
        param_table["data"]["my_tgp_id"] = login_model.im_login_infomation.friend_id;
        param_table["data"]["qq_friend_list"] = qq_friend_control.qq_friend_list;
        param_table["data"]["qq_group_info"] = qq_friend_control.qq_group_info;
       web_utility.lua_call_js_by_id("push_qq_friend_list",base_utility.table_to_json(param_table), qq_friend_view:get_web_ctrl_id());
    end;

    open_qq_group = function(json_str)
        base_utility.log("[qq_friend_control] open_qq_group : "..json_str, true);
        local param_tbl = base_utility.json_to_table(json_str);
        for index, friend in ipairs(qq_friend_control.qq_friend_list) do
            if friend.qq_group_id == param_tbl.group_id then
                qq_friend_control.qq_friend_list[index].is_new = 0;
            end
        end
        comm_center.call_service("im_app_service_clear_group_qq_friend_new_status", {qq_group_id = param_tbl.group_id});
    end;

    add_qq_friend = function(json_str)
        base_utility.log("[qq_friend_control] add_qq_friend : "..json_str, true);
        local param_tbl = base_utility.json_to_table(json_str);

        local ext_info = {};
        ext_info["type"] = 1 --friend_info["type"]; --关系:qq好友 游戏好友, 3-未知, 如果有最近游戏字段， 填写游戏id ext_info["game_id"] =， 没有则不填写

        local from = {};
        if login_model.im_login_infomation then 
            from = social_network_control.get_my_remark_info();
            base_utility.log("[qq_friend_control.add_frien] ext len:"..tostring(string.len(base_utility.table_to_json(from))), true);
        end
        ext_info["f"] = from;

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "qq_friend_control.add_friend_rsp");

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:add_friend(tostring(param_tbl.tgp_id),base_utility.table_to_json(ext_info));
    end;

    add_friend_rsp = function(event_id, json_str)
        base_utility.log("[qq_friend_control] rsp:"..json_str, true);
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "qq_friend_control.add_friend_rsp");
    end;

    show_personal_center = function(json_str)
        base_utility.log("[qq_friend_control] open_personal_center : "..json_str, true);
        local param_tbl = base_utility.json_to_table(json_str);

        local tbl = {friend_id = tostring(param_tbl.friend_id)};
        event_center.send_event(group_event.SHOW_PERSONAL_CENTER,base_utility.table_to_json(tbl));
    end;

    close = function(json_str)
        qq_friend_view:show(false);
    end;
}

qq_friend_control.init();